﻿using System.Windows;

namespace Motar.Desktop.Controls
{

    /// <summary>
    /// 
    /// </summary>
    public static class Interaction
    {

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty BehaviorsProperty = DependencyProperty.RegisterAttached("Behaviors", typeof(BehaviorCollection), typeof(Interaction), new UIPropertyMetadata(null, new PropertyChangedCallback(OnBehaviorsChanged)));

        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static BehaviorCollection GetBehaviors(DependencyObject d)
        {
            return (BehaviorCollection)d.GetValue(BehaviorsProperty);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="value"></param>
        public static void SetBehaviors(DependencyObject d, BehaviorCollection value)
        {
            d.SetValue(BehaviorsProperty, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnBehaviorsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var behaviors = System.Windows.Interactivity.Interaction.GetBehaviors(d);

            BehaviorCollection removedBehaviors = (BehaviorCollection)e.OldValue;

            if (removedBehaviors != null && removedBehaviors.Count > 0)
            {
                foreach (var behavior in removedBehaviors)
                {
                    behaviors.Remove(behavior);
                }
            }

            BehaviorCollection addedBehaviors = (BehaviorCollection)e.NewValue;

            if (addedBehaviors != null && addedBehaviors.Count > 0)
            {
                foreach (var behavior in addedBehaviors)
                {
                    behaviors.Add(behavior);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty TriggersProperty = DependencyProperty.RegisterAttached("Triggers", typeof(TriggerCollection), typeof(Interaction), new UIPropertyMetadata(null, new PropertyChangedCallback(OnTriggersChanged)));

        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static TriggerCollection GetTriggers(DependencyObject d)
        {
            return (TriggerCollection)d.GetValue(TriggersProperty);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="value"></param>
        public static void SetTriggers(DependencyObject d, TriggerCollection value)
        {
            d.SetValue(TriggersProperty, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnTriggersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var triggers = System.Windows.Interactivity.Interaction.GetTriggers(d);

            TriggerCollection removedTriggers = (TriggerCollection)e.OldValue;

            if (removedTriggers != null && removedTriggers.Count > 0)
            {
                foreach (var trigger in removedTriggers)
                {
                    triggers.Remove(trigger);
                }
            }

            TriggerCollection addedTriggers = (TriggerCollection)e.NewValue;

            if (addedTriggers != null && addedTriggers.Count > 0)
            {
                foreach (var trigger in addedTriggers)
                {
                    triggers.Add(trigger);
                }
            }
        }
    }
}
